/**
 * 添加或者修改页面
 */
var TProductionProcessInfoDlg = {
    data: {
        processDec: "",
        businessLicense: "",
        eiaApproval: "",
        eiaAcceptance: "",
        pollutionPermit: "",
        floorPlan: "",
        msds: "",
        flowChart: ""
    }
};

layui.extend({
    tinymce: '../../tinymce/tinymce'
}).use(['form','upload', 'admin', 'ax','tinymce'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var upload = layui.upload;
    var tinymce = layui.tinymce;

    //让当前iframe弹层高度适应
    // admin.iframeAuto();

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/tProductionProcess/addItem", function (data) {
            if (data.code == 200) {
                Feng.success("添加成功！");
            }else{
                Feng.error("添加失败！" + data.message)
            }

            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);

            //关掉对话框
            admin.closeThisDialog();

        }, function (data) {
            Feng.error("添加失败！" + data.message)
        });
        data.field.businessLicense = $('#urlImg').attr('src');
        data.field.eiaApproval = $('#eiaApprovalImg').attr('src');
        data.field.eiaAcceptance = $('#eiaAcceptanceImg').attr('src');
        data.field.pollutionPermit = $('#pollutionPermitImg').attr('src');
        data.field.floorPlan = $('#floorPlanImg').attr('src');
        data.field.msds = $('#msdsImg').attr('src');
        data.field.flowChart = $('#flowChartImg').attr('src');
        data.field.processDec = tinymce.get('#processDec').getContent();

        ajax.set(data.field);
        ajax.start();

        return false;
    });


    /*监听公司名称*/
    var ajax1 = new $ax(Feng.ctxPath + "/tBasicinfo/basicList");
    var companyList = ajax1.start();
    console.log(companyList);
    for (var i = 0 ; i < companyList.length ; i++){
        $("#companyId").append("<option value=\"" + companyList[i].id + "\">" + companyList[i].companyName + "</option>");
    }
    //重新渲染
    layui.form.render("select");


    /**
     * 上传营业执照
     */
    var uploadInst = upload.render({
        elem: '#urlImageBtn'
        ,url: Feng.ctxPath+'/ali/uploadFile'
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                loading = layer.load(0, {
                    shade: false,
                    time: 10*1000
                })
            });
        }
        ,done: function(res){
            console.log(res);
            layer.close(loading);
            $('#urlImg').attr('src', res.data);
            $("#urlImg").after('<input type="hidden" name="url" value="' + res.data + '" />');
            //上传成功
        }
        ,error: function(){
            //演示失败状态，并实现重传
            var demoText = $('#urlImg');
            layer.close(loading);
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a '+'class="layui-btn layui-btn-xs demo-reload">重试</ a>');
            demoText.find('.demo-reload').on('click', function(){
                uploadInst.upload();
            });
        }
    });


    /**
     * 上传环评批复
     */
    var uploadInst = upload.render({
        elem: '#eiaApprovalClick'
        ,url: Feng.ctxPath+'/ali/uploadFile'
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                loading = layer.load(0, {
                    shade: false,
                    time: 10*1000
                })
            });
        }
        ,done: function(res){
            console.log(res);
            layer.close(loading);
            $('#eiaApprovalImg').attr('src', res.data);
            $("#eiaApprovalImg").after('<input type="hidden" name="eiaApproval" value="' + res.data + '" />');
            //上传成功
        }
        ,error: function(){
            //演示失败状态，并实现重传
            var demoText = $('#eiaApprovalImg');
            layer.close(loading);
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a '+'class="layui-btn layui-btn-xs demo-reload">重试</ a>');
            demoText.find('.demo-reload').on('click', function(){
                uploadInst.upload();
            });
        }
    });


    /**
     * 上传环评验收
     */
    var uploadInst = upload.render({
        elem: '#eiaAcceptanceClick'
        ,url: Feng.ctxPath+'/ali/uploadFile'
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                loading = layer.load(0, {
                    shade: false,
                    time: 10*1000
                })
            });
        }
        ,done: function(res){
            console.log(res);
            layer.close(loading);
            $('#eiaAcceptanceImg').attr('src', res.data);
            $("#eiaAcceptanceImg").after('<input type="hidden" name="eiaAcceptance" value="' + res.data + '" />');
            //上传成功
        }
        ,error: function(){
            //演示失败状态，并实现重传
            var demoText = $('#eiaAcceptanceImg');
            layer.close(loading);
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a '+'class="layui-btn layui-btn-xs demo-reload">重试</ a>');
            demoText.find('.demo-reload').on('click', function(){
                uploadInst.upload();
            });
        }
    });


    /**
     * 上传排污许可证
     */
    var uploadInst = upload.render({
        elem: '#pollutionPermitClick'
        ,url: Feng.ctxPath+'/ali/uploadFile'
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                loading = layer.load(0, {
                    shade: false,
                    time: 10*1000
                })
            });
        }
        ,done: function(res){
            console.log(res);
            layer.close(loading);
            $('#pollutionPermitImg').attr('src', res.data);
            $("#pollutionPermitImg").after('<input type="hidden" name="pollutionPermit" value="' + res.data + '" />');
            //上传成功
        }
        ,error: function(){
            //演示失败状态，并实现重传
            var demoText = $('#pollutionPermitImg');
            layer.close(loading);
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a '+'class="layui-btn layui-btn-xs demo-reload">重试</ a>');
            demoText.find('.demo-reload').on('click', function(){
                uploadInst.upload();
            });
        }
    });


    /**
     * 上传厂区平面图
     */
    var uploadInst = upload.render({
        elem: '#floorPlanClick'
        ,url: Feng.ctxPath+'/ali/uploadFile'
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                loading = layer.load(0, {
                    shade: false,
                    time: 10*1000
                })
            });
        }
        ,done: function(res){
            console.log(res);
            layer.close(loading);
            $('#floorPlanImg').attr('src', res.data);
            $("#floorPlanImg").after('<input type="hidden" name="floorPlan" value="' + res.data + '" />');
            //上传成功
        }
        ,error: function(){
            //演示失败状态，并实现重传
            var demoText = $('#floorPlanImg');
            layer.close(loading);
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a '+'class="layui-btn layui-btn-xs demo-reload">重试</ a>');
            demoText.find('.demo-reload').on('click', function(){
                uploadInst.upload();
            });
        }
    });

    //涉VOCs原辅料MSDS
    var uploadInst = upload.render({
        elem: '#msdsClick'
        ,url: Feng.ctxPath+'/ali/uploadFile'
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                loading = layer.load(0, {
                    shade: false,
                    time: 10*1000
                })
            });
        }
        ,done: function(res){
            console.log(res);
            layer.close(loading);
            $('#msdsImg').attr('src', res.data);
            $("#msdsImg").after('<input type="hidden" name="msds" value="' + res.data + '" />');
            //上传成功
        }
        ,error: function(){
            //演示失败状态，并实现重传
            var demoText = $('#msdsImg');
            layer.close(loading);
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a '+'class="layui-btn layui-btn-xs demo-reload">重试</ a>');
            demoText.find('.demo-reload').on('click', function(){
                uploadInst.upload();
            });
        }
    });


    //生产工艺流程图
    var uploadInst = upload.render({
        elem: '#flowChartClick'
        ,url: Feng.ctxPath+'/ali/uploadFile'

        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                loading = layer.load(0, {
                    shade: false,
                    time: 10*1000
                })
            });
        }
        ,done: function(res){
            console.log(res);
            layer.close(loading);
            $('#flowChartImg').attr('src', res.data);
            $('#flowChart').attr("value", res.data);
            // $("#flowChartImg").after('<input type="hidden" name="flowChart" value="' + res.data + '" />');
            //上传成功
        }
        ,error: function(){
            //演示失败状态，并实现重传
            var demoText = $('#flowChartImg');
            layer.close(loading);
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a '+'class="layui-btn layui-btn-xs demo-reload">重试</ a>');
            demoText.find('.demo-reload').on('click', function(){
                uploadInst.upload();
            });
        }
    });



    //from表单提交 插入data中


    tinymce.init({
        elem: "#processDec",
        height: 500,
        plugins:
            "link lists image code table colorpicker textcolor wordcount contextmenu",
        toolbar:
            "bold italic underline strikethrough | fontsizeselect | fontselect | forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist | outdent indent blockquote | undo redo | link unlink image code | removeformat",
        branding: false,
        images_upload_handler: function (blobInfo, success, failure) {
            var file = new FormData();
            file.append('file', blobInfo.blob(), blobInfo.filename());
            $.ajax({
                //图片/文件上传地址
                url: Feng.ctxPath+'/ali/uploadFile',
                type: "post",
                data: file,
                processData: false,
                contentType: false,
                success: function(data) {
                    success(data.data);
                },
                error: function(e) {
                    alert("图片上传失败");
                }
            });
        }
    });

});